package charts;

import java.awt.Color;
import java.util.Iterator;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

import summary.GradientStatistics;
import summary.InstantStatistics;
import summary.Analyzer;
import summary.NeighborInstantStatistics;
import summary.ReferenceStatistics;


public class NeighborChart{
	
	public NeighborChart(Vector<NeighborInstantStatistics> statistics) {
	    
	    final JFreeChart gradientChart = createGradientChart("Maximum Local Skew",statistics);

	    final ChartPanel gradientPanel = new ChartPanel(gradientChart);
	    gradientPanel.setPreferredSize(new java.awt.Dimension(500, 270));
	    ApplicationFrame a4 = new ApplicationFrame("");
	    a4.setContentPane(gradientPanel);	    
	    a4.pack();	    
	    a4.setVisible(true);
	}
	
	private JFreeChart createGradientChart(String title, Vector<NeighborInstantStatistics> statistics) {
	    
		final XYSeries local = new XYSeries("Skew between nodes 19 and 20");
	
	    final ValueAxis time = new NumberAxis("Time (second)");
	    final ValueAxis skew = new NumberAxis("Skew (microsecond)");
		
		for (Iterator<NeighborInstantStatistics> iterator = statistics.iterator(); iterator.hasNext();) {
			NeighborInstantStatistics data = (NeighborInstantStatistics) iterator.next();
						
			local.add(data.second,data.skew);	
		}			
		
        final XYPlot plot = new XYPlot(new XYSeriesCollection(local), time, skew, new StandardXYItemRenderer());

        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.getRenderer(0).setPaint(Color.BLUE);
        
        return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
	}	
	

}
